﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Progetto
{
    public partial class Statistics_Form : Form
    {
        private List<Place> places_List;
        private string currentPlace;
        ListView Places_listView;

        public Statistics_Form(List<Place> places_List, string currentPlace, ListView Places_listView)
        {
            InitializeComponent();

            this.places_List = places_List;
            this.currentPlace = currentPlace;
            this.Places_listView = Places_listView;
            
            InitializeControls(this.places_List);
        }

        private void InitializeControls(List<Place> places) {
            
            for(int i = 0; i < places.Count; i++) {
                Places_comboBox.Items.Add(places[i].getPlaceName());
            }
        }

        private void Places_comboBox_KeyUp(object sender, KeyEventArgs e) {

            int index;
            string actual;
            string found;
	
            // Do nothing for certain keys, such as navigation keys.
            if ((e.KeyCode == Keys.Back) ||
	            (e.KeyCode == Keys.Left) ||
	            (e.KeyCode == Keys.Right) ||
	            (e.KeyCode == Keys.Up) ||
	            (e.KeyCode == Keys.Down) ||
	            (e.KeyCode == Keys.Delete) ||
	            (e.KeyCode == Keys.PageUp) ||
	            (e.KeyCode == Keys.PageDown) ||
	            (e.KeyCode == Keys.Home) ||
	            (e.KeyCode == Keys.End)) {
	            return;
            }

            // Store the actual text that has been typed.
            actual = this.Places_comboBox.Text;

            // Find the first match for the typed value.
            index = this.Places_comboBox.FindString(actual);

            // Get the text of the first match.
            if (index > -1) {
	            found = this.Places_comboBox.Items[index].ToString();
			
	            // Select this item from the list.
	            this.Places_comboBox.SelectedIndex = index;
				
	            // Select the portion of the text that was automatically
	            // added so that additional typing replaces it.
	            this.Places_comboBox.SelectionStart = actual.Length;
	            this.Places_comboBox.SelectionLength = found.Length;

                // initialize other controls
                string placeName = Places_comboBox.Items[index].ToString();
                for(int i = 0; i < places_List.Count; i++) {
                    if(placeName == places_List[i].getPlaceName()) {

                        Dictionary<string, UInt32> network_list = places_List[i].getNetworkList();
                        TimeSpan ts = places_List[i].getTimeOfStay();
                        List<string> actions = places_List[i].getActionList();

                        // Fill listView control
                        Networks_listView.Items.Clear();
                        foreach (string networkName in network_list.Keys) {
                            ListViewItem lv = new ListViewItem(networkName);
                            lv.SubItems.Add(network_list[networkName].ToString() + " %");
                            Networks_listView.Items.Add(lv);

                        }

                        // Fill richTextBox control
                        Duration_richTextBox.Clear();
                        Duration_richTextBox.Text = ts.ToString();

                        // Fill listBox control
                        Actions_listBox.Items.Clear();
                        foreach(string action in actions)
                            Actions_listBox.Items.Add(action);

                        return;

                    }
                }
                
            }
        }

        private void Places_comboBox_SelectedIndexChanged(object sender, EventArgs e) {

            Edit_button.Enabled = true;
            AddActions_button.Enabled = true;
            Run_Button.Enabled = true;
            
            int index = this.Places_comboBox.SelectedIndex;

            string placeName = Places_comboBox.Items[index].ToString();
            for (int i = 0; i < places_List.Count; i++) {

                if (placeName == places_List[i].getPlaceName()) {

                    Dictionary<string, UInt32> network_list = places_List[i].getNetworkList();
                    TimeSpan ts = places_List[i].getTimeOfStay();
                    List<string> actions = places_List[i].getActionList();

                    // Fill listView control
                    Networks_listView.Items.Clear();
                    foreach (string networkName in network_list.Keys)
                    {
                        ListViewItem lv = new ListViewItem(networkName);
                        lv.SubItems.Add(network_list[networkName].ToString() + " %");
                        Networks_listView.Items.Add(lv);

                    }

                    // Fill richTextBox control
                    Duration_richTextBox.Clear();
                    Duration_richTextBox.Text = ts.ToString();

                    // Fill listBox control
                    Actions_listBox.Items.Clear();
                    foreach (string action in actions)
                        Actions_listBox.Items.Add(action);

                    return;

                }
            }
        }

        private void AddActions_button_Click(object sender, EventArgs e) {

            int index = this.Places_comboBox.SelectedIndex;

            if(index <= -1) {
                MessageBox.Show("Select a place first.", "Attention", MessageBoxButtons.OK);
                return;
            }

            string placeName = Places_comboBox.Items[index].ToString();

            NewAction_Form dialog = new NewAction_Form();

            if (dialog.ShowDialog(this) == DialogResult.OK) {
                string actionName = dialog.insertedText;

                for (int i = 0; i < places_List.Count; i++) {
                    if (placeName == places_List[i].getPlaceName()) {
                        if(!places_List[i].getActionList().Contains(actionName)) {
                            places_List[i].setActionList(actionName);

                            // Fill listBox control
                            Actions_listBox.Items.Clear();
                            foreach (string action in places_List[i].getActionList())
                                Actions_listBox.Items.Add(action);
                        }
                        else
                            MessageBox.Show("Action already existent!", "Attention", MessageBoxButtons.OK);

                        return;

                    }
                }
            }
            else
                return;

        }

        private void Run_Button_Click(object sender, EventArgs e) {
            int index = this.Places_comboBox.SelectedIndex;

            if (index <= -1) {
                MessageBox.Show("Select a place first.", "Attention", MessageBoxButtons.OK);
                return;
            }
            string placeName = Places_comboBox.Items[index].ToString();

            for (int i = 0; i < places_List.Count; i++) {
                if (placeName == places_List[i].getPlaceName()) {
                    foreach(string action in places_List[i].getActionList()) {
                        if(action == "Firewall") {
                            Process process = new Process();
                            process.StartInfo.FileName = @"C:\Users\Alessandro Avila\Polito\Primo anno\Programmazione di sistema\C++\AutoIt executable files\firewall.exe";
                            process.Start();
                        }
                        else if(action == "Networks") {
                            // nop (al momento)
                        }
                        else {
                            Process process = new Process();
                            process.StartInfo.FileName = action;
                            process.Start();
                        }
                    }
                }
            }
        }

        private void Edit_button_Click(object sender, EventArgs e) {
            int index = this.Places_comboBox.SelectedIndex;

            if(index <= -1) {
                MessageBox.Show("Select a place first.", "Attention", MessageBoxButtons.OK);
                return;
            }
            else {
                string oldPlaceName = Places_comboBox.Items[index].ToString();
                EditName editName = new EditName();

                if (editName.ShowDialog(this) == DialogResult.OK) {

                    string newPlaceName = editName.insertedText;
                    
                    if(oldPlaceName == newPlaceName) {
                        MessageBox.Show("You have typed the same previous name. No modifications.", "Attention", MessageBoxButtons.OK);
                        return;
                    }
                    else {
                        for(int i = 0; i < places_List.Count; i++) {
                            if(newPlaceName == places_List[i].getPlaceName()) {
                                MessageBox.Show("You have typed a place name already existent. No modifications.", "Attention", MessageBoxButtons.OK);
                                return;
                            }
                        }
                    }
                    
                    if (oldPlaceName == currentPlace) {
                        currentPlace = newPlaceName;
                        Main_Form.currentPlace = newPlaceName;
                    }

                    for (int i = 0; i < places_List.Count; i++) {
                        if (oldPlaceName == places_List[i].getPlaceName())
                            places_List[i].setPlaceName(newPlaceName);
                    }
                    
                    // update listView
                    updatePlaces_listView();

                    // update combobox
                    Places_comboBox.Items.Clear();
                    for (int j = 0; j < places_List.Count; j++)
                        Places_comboBox.Items.Add(places_List[j].getPlaceName());
                    Places_comboBox.Text = newPlaceName;
                    
                    // text selection
                    Places_comboBox.SelectionStart = 0;
                    Places_comboBox.SelectionLength = newPlaceName.Length;
                }
                else
                    return;
            }

        }

        private void updatePlaces_listView() {
            
            for(int i = 0; i < places_List.Count; i++) {
                ListViewItem lv = Places_listView.Items[i];
                lv.SubItems[1].Text = places_List[i].getPlaceName();
            }
        }

    }
}